#include "msgprocessthread.h"
#include <QDebug>

MsgProcessThread::MsgProcessThread(QQueue<UiMsg> *msgQueue,QMutex *uiMutex,
                                   QWaitCondition *condUiNotify)
{
    this->m_pMsgQueue = msgQueue;
    this->m_pUiMutex = uiMutex;
    this->m_pCondUiNotify = condUiNotify;
}

MsgProcessThread::~MsgProcessThread()
{

}

void MsgProcessThread::run()
{
    while(!isInterruptionRequested()){

        m_pUiMutex->lock();

        if(m_pMsgQueue->isEmpty()){
            qDebug() << " message queue is empty wait for message in thread " << QThread::currentThreadId();
            m_pCondUiNotify->wait(m_pUiMutex);
        }

        /* if ui thread interrupt while waiting for message */
        if(isInterruptionRequested()){
            qDebug() << " message thread interrupted by ui thread";
            break;
        }

        UiMsg uiMsg = m_pMsgQueue->first();
        m_pMsgQueue->pop_front();

        qDebug() << " get ui message from ui thread " << uiMsg.msgSendThreadId
                 << " uiMsg no " << uiMsg.msgNo
                 << " uiMsg data " << uiMsg.msgData;

        m_pUiMutex->unlock();
    };
}
